package com.example.schooljava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.schooljava.pojo.bo.AddClassBO;
import com.example.schooljava.pojo.bo.ClassBO;
import com.example.schooljava.pojo.entity.Class;
import com.example.schooljava.mapper.ClassMapper;
import com.example.schooljava.service.ClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.schooljava.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LDH
 * @since 2022/08/11
 */
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {

    @Autowired
    private ClassMapper classMapper;


    /**
     * 功能描述: 班级添加功能<br>
     * @Param: [c]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/17 16:54
     */
    @Override
    public Result inPutClass(AddClassBO addClassBO) {
        //创建条件构造器
        QueryWrapper<Class> wrapper = new QueryWrapper<>();
        // 判断要创建的班级信息中，班级和年级是否同时存在，存在则不添加
        wrapper.eq("grade",addClassBO.getGrade()).eq("name",addClassBO.getName());
        if (list(wrapper).size()!=0){
            return new Result<>().OK_Back(false,"此班级已存在",classMapper.getClass(new ClassBO()));
        }
        Class c = new Class();
        c.setAssistant(addClassBO.getAssistant());
        c.setGrade(addClassBO.getGrade());
        c.setHeadmaster(addClassBO.getHeadmaster());
        c.setName(addClassBO.getName());
        save(c);
        return new Result<>().OK_Back(true,"班级添加成功",classMapper.getClass(new ClassBO()));
    }

    /**
     * 功能描述: 获取所有班级信息<br>
     * @Param: [query]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/17 17:40
     */
    @Override
    public Result getClass(ClassBO classBO) {
        return new Result<>().Huoqu_OK(classMapper.getClass(classBO));
    }

    /**
     * 功能描述: 删除班级<br>
     * @Param: [list]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/17 17:43
     */
    @Override
    public Result delClass(ArrayList<String> list) {
        for (String l : list) {
            for (Class c : list()) {
                if (Long.parseLong(l) == c.getId())
                    removeById(Long.parseLong(l));
            }
        }
        return new Result<>().Huoqu_OK(classMapper.getClass(new ClassBO()));
    }

    /**
     * 功能描述: 获取还没当班级助理的教师信息<br>
     * @Param: []
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/17 17:43
     */
    @Override
    public Result getAssistant() {
        return new Result<>().Huoqu_OK(classMapper.getAssistant());
    }


    /**
     * 功能描述: 获取还没当班主任的教师信息<br>
     * @Param: []
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/17 17:43
     */
    @Override
    public Result getHeadmaster() {
        return new Result<>().Huoqu_OK(classMapper.getHeadmaster());
    }

    /**
     * 功能描述: 编辑班级信息<br>
     * @Param: [query]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/17 22:01
     */
    @Override
    public Result editClass(AddClassBO addClassBO) {
        Class c = new Class();
        c.setId(addClassBO.getId());
        if (addClassBO.getAssistant()!= null)
            c.setAssistant(addClassBO.getAssistant());
        if (addClassBO.getHeadmaster() != null)
            c.setHeadmaster(addClassBO.getHeadmaster());
        c.setGrade(addClassBO.getGrade());
        c.setName(addClassBO.getName());
        updateById(c);
        return new Result<>().OK_Back(true,"班级修改成功！",classMapper.getClass(new ClassBO()));
    }

    @Override
    public Result getAllClass() {
        return new Result<>().Huoqu_OK(classMapper.getAllClass());
    }
}
